local Msg = require("core.Msg")
local PresentExcel = require("excel.present")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")

ACT_TYPE_HERO_RISE_REWARD = 1       -- 奖励
ACT_TYPE_HERO_RISE_TASK = 2         -- 任务

ACT_TYPE_HERO_RISE_TASK_TYPE1 = 1   -- 每日任务
ACT_TYPE_HERO_RISE_TASK_TYPE2 = 2   -- 每周任务
ACT_TYPE_HERO_RISE_TASK_TYPE3 = 3   -- 终极任务


ACT_TYPE_HERO_RISE_TASK_ID1 = 1   -- 
ACT_TYPE_HERO_RISE_TASK_ID2 = 2   -- 
ACT_TYPE_HERO_RISE_TASK_ID3 = 3   -- 
ACT_TYPE_HERO_RISE_TASK_ID4 = 4   -- 
ACT_TYPE_HERO_RISE_TASK_ID5 = 5   -- 
ACT_TYPE_HERO_RISE_TASK_ID6 = 6   -- 
ACT_TYPE_HERO_RISE_TASK_ID7 = 7   -- 
ACT_TYPE_HERO_RISE_TASK_ID8 = 8   -- 
ACT_TYPE_HERO_RISE_TASK_ID9 = 9   -- 
ACT_TYPE_HERO_RISE_TASK_ID10 = 10   -- 
ACT_TYPE_HERO_RISE_TASK_ID11 = 11   -- 
ACT_TYPE_HERO_RISE_TASK_ID12 = 12   -- 
ACT_TYPE_HERO_RISE_TASK_ID13 = 13   -- 
ACT_TYPE_HERO_RISE_TASK_ID14 = 14   -- 
ACT_TYPE_HERO_RISE_TASK_ID15 = 15   -- 
ACT_TYPE_HERO_RISE_TASK_ID16 = 16   -- 
ACT_TYPE_HERO_RISE_TASK_ID17 = 17   -- 
ACT_TYPE_HERO_RISE_TASK_ID18 = 18   -- 
ACT_TYPE_HERO_RISE_TASK_ID19 = 19   -- 
-- 登录推送
function onLogin(human)
    local msgRet = Msg.gc.GC_ACT_HERO_RISE_OPEN
    msgRet.isOpen = checkActIsOpen(human)
    Msg.send(msgRet,human.fd)
    if checkActIsOpen(human) == 1 then
        initHeroRiseDB(human)
    end
end

-- 检查活动是否开启
function checkActIsOpen(human)
    if true then
        return 0
    end

    if human.db.lv < 15 then
        return 0
    end

    -- todo
    return 1
end

-- 初始化数据
function initHeroRiseDB(human)
    if human.db.heroRise ~= nil then
        return
    end

    local now = os.time()
    human.db.heroRise = {}
    human.db.heroRise.startTs = now
    human.db.heroRise.endTs = now + 30*24*60*60
    human.db.heroRise.lv = 0
    human.db.heroRise.exp = 0
    human.db.heroRise.rajah = 0
    human.db.heroRise.task = {}
    human.db.heroRise.get = {}
end

-- 任务构造
function makeHeroRiseTask(human,net,data)
    net.id = data.id
    net.name = data.name
    net.desc = data.desc
    net.exp = data.exp
    net.needCnt = data.needCnt
    net.nowCnt = human.db.heroRise.task[data.id] and human.db.heroRise.task[data.id].cnt or 0
    net.state = human.db.heroRise.task[data.id] and human.db.heroRise.task[data.id].state or 0
end

-- 查询英雄崛起活动
function heroRiseQuery(human)
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    if human.db.heroRise == nil then
        return
    end

    local config = PresentExcel.heroRiseReward

    local msgRet = Msg.gc.GC_ACT_HERO_RISE_QUERY_REWARD
    msgRet.riseType = human.db.heroRise.rajah 
    msgRet.riseLv = human.db.heroRise.lv 
    local needExp = 0
    if human.db.heroRise.lv > 40 then
        needExp = config[40].exp
    else
        needExp = config[human.db.heroRise.lv].exp
    end
    msgRet.needExp = needExp
    msgRet.exp = human.db.heroRise.exp

    local len = #config[0].rajahReward
    for i = 1,len do
        local v = config[0].rajahReward[i]
        Grid.makeItem(msgRet.unLockReward[i],v[1],v[2])
    end
    msgRet.unLockReward[0] = len

    len = #config
    local count = 0
    for i = 1,len do
        count = count + 1
        local heroLen = #config[i].baseReward

        local net = msgRet.heroReward[count]
        net.lv = config[i].lv

        if human.db.heroRise.lv < i then
            net.lock = 0
        else
            net.lock = 1
        end

        for j = 1,heroLen do
            local v = config[i].baseReward[j]
            Grid.makeItem(net.item[j],v[1],v[2])
        end
        net.item[0] = heroLen

        local rNet = msgRet.rajahReward[count]
        rNet.lv = config[i].lv

        if human.db.heroRise.lv < i or human.db.heroRise.rajah == 0 then
            rNet.lock = 0
        else
            rNet.lock = 1
        end

        heroLen = #config[i].exReward
        for j = 1,heroLen do
            local v = config[i].exReward[j]
            Grid.makeItem(rNet.item[j],v[1],v[2])
        end
        rNet.item[0] = heroLen
    end
    msgRet.heroReward[0] = count
    msgRet.rajahReward[0] = count

    local taskConfig = PresentExcel.heroRiseTask
    local taskLen = #taskConfig
    for i = 1,taskLen do
        local v = taskConfig[i]
        if v.type == ACT_TYPE_HERO_RISE_TASK_TYPE1 then
            msgRet.dailyTask[0] = msgRet.dailyTask[0] + 1
            makeHeroRiseTask(human,msgRet.dailyTask[msgRet.dailyTask[0]],v)
        elseif v.type == ACT_TYPE_HERO_RISE_TASK_TYPE2 then
            msgRet.weekTask[0] = msgRet.weekTask[0] + 1
            makeHeroRiseTask(human,msgRet.weekTask[msgRet.weekTask[0]],v)
        elseif v.type == ACT_TYPE_HERO_RISE_TASK_TYPE3 then
            msgRet.ultimateTask[0] = msgRet.ultimateTask[0] + 1
            makeHeroRiseTask(human,msgRet.ultimateTask[msgRet.ultimateTask[0]],v)
        end
    end

    Msg.send(msgRet,human.fd)
end

-- 领取任务奖励
function getTaskReward(human,taskId)
    -- 活动不处于开启状态
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    -- 校验任务是否已达成
    if human.db.heroRise.task[taskId] == nil or
       human.db.heroRise.task[taskId].state ~= 1 then
        return
    end

    -- 清除达成信息
    human.db.heroRise.task[taskId].state = 2

    -- 获取奖励
    local config = PresentExcel.heroRiseTask[taskId]
    addExpTouch(human,config.exp)

    heroRiseQuery(human)
end

-- 任务回调
function taskTouch(human,taskId,condition)
    -- 活动不处于开启状态
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    local config = PresentExcel.heroRiseTask[taskId]
    -- 条件不满足
    if condition ~= config.condition and config.condition ~= 0 then
        return
    end

    -- 初始化任务数据
    if human.db.heroRise.task[taskId] == nil then
        human.db.heroRise.task[taskId] = {}
        human.db.heroRise.task[taskId].cnt = 0
        human.db.heroRise.task[taskId].state = 0
    end

    -- 校验任务是否已完成
    if human.db.heroRise.task[taskId].state ~= 0 then
        return
    end

    human.db.heroRise.task[taskId].cnt = human.db.heroRise.task[taskId].cnt + 1
    if human.db.heroRise.task[taskId].cnt == config.needCnt then
        human.db.heroRise.task[taskId].state = 1
    end
end

-- 获取经验回调
function addExpTouch(human,exp)
    -- 活动不处于开启状态
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    local lv = human.db.heroRise.lv >= 40 and 40 or human.db.heroRise.lv
    local config = PresentExcel.heroRiseReward[lv]

    human.db.heroRise.exp = human.db.heroRise.exp + exp

    local count = 0
    local lvUp = nil
    while true do
        count = count + 1
        if human.db.heroRise.exp >= config.exp then
            human.db.heroRise.exp = human.db.heroRise.exp - config.exp
            human.db.heroRise.lv = human.db.heroRise.lv + 1
            lvUp = 1
        else
            break
        end

        -- 防止死循环
        if count >= 100 then
            break
        end
    end

    if lvUp ~= nil then
        lvUpSendReward(human)
    end
end

-- 升级发放奖励回调
function lvUpSendReward(human)
    -- 活动不处于开启状态
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    local msgRet = Msg.gc.GC_ACT_HERO_RISE_LV_UP
    local item = {}
    for i = 1,human.db.heroRise.lv do
        if human.db.heroRise.get[i] == nil then
            human.db.heroRise.get[i] = 1

            local baseConfig = PresentExcel.baseReward[i]
            local len = #baseConfig
            for j = 1,len do
                if item[baseConfig[j][1]] == nil then
                    item[baseConfig[j][1]] = baseConfig[j][2]
                else
                    item[baseConfig[j][1]] = item[baseConfig[j][1]] + baseConfig[j][2]
                end
            end

            if human.db.heroRise.rajah == 1 then
                local exConfig = PresentExcel.exReward[i]
                local exLen = #exConfig
                for j = 1,exLen do
                    if item[exConfig[j][1]] == nil then
                        item[exConfig[j][1]] = exConfig[j][2]
                    else
                        item[exConfig[j][1]] = item[exConfig[j][1]] + exConfig[j][2]
                    end
                end
            end
        end
    end
    BagLogic.addItemList(human, item, "hero_rise")
    local count = 0
    for k,v in pairs(item) do
        count = count + 1
        Grid.makeItem(msgRet.item[count],k,v)
    end
    msgRet.item[0] = count
    msgRet.lv = human.db.heroRise.lv
    msgRet.rajah = human.db.heroRise.rajah
    Msg.send(msgRet,human.fd)
end

-- 购买王侯回调
function buyRajah(human)
    -- 活动不处于开启状态
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    -- 已经是王侯
    if human.db.heroRise.rajah ~= 0 then
        return
    end

    human.db.heroRise.rajah = 1

    local config = PresentExcel.heroRiseReward
    local len = #config[0].rajahReward
    BagLogic.cleanMomentItemList()
    for i = 1,len do
        BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config[0].rajahReward[i][1],config[0].rajahReward[i][2])
    end

    for i = 1,human.db.heroRise.lv do
        local exLen = #config[i].exReward
        for j = 1,len do
            BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config[i].exReward[j][1],config[i].exReward[j][2])
        end
    end
    BagLogic.addMomentItemList(human, "hero_rise")
end